package view;

import app.App;
import model.User;

import javax.swing.*;
import java.awt.*;

public class ManageView extends JFrame{

    private final static int INIT_W = 1000;  //窗体初始宽度
    private final static int INIT_H = 650;  //窗体初始高度

    private JPanel contentPane;
    private JTabbedPane tp_categories;

    public ManageView() {
        App.systemPane = new SystemPane();
        App.warehousePane = new WarehousePane();
        App.statisticsPane = new StatisticsPane();

        tp_categories.addTab("系统管理", App.systemPane.getContentPane());
        tp_categories.addTab("仓库管理", App.warehousePane.getContentPane());
        tp_categories.addTab("统计信息", App.statisticsPane.getContentPane());

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("超市管理界面");
        setMinimumSize(new Dimension(INIT_W,INIT_H));
        setContentPane(contentPane);
        pack();
        setVisible(true);
        //居中
        Point p = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
        setBounds(p.x - INIT_W / 2, p.y - INIT_H / 2, INIT_W, INIT_H);

        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu menu1 = new JMenu("  操作  ");
        menuBar.add(menu1);
        JMenuItem item1 = new JMenuItem("  打开零售界面  ");
        JMenuItem item2 = new JMenuItem("  退出登录  ");
        JMenuItem item3 = new JMenuItem("  退出  ");
        menu1.add(item1);
        menu1.addSeparator();
        menu1.add(item2);
        menu1.add(item3);
        //打开零售界面
        item1.addActionListener(e -> {
            if (App.checkSalePermission()) {
                if (App.saleView == null || !App.saleView.isShowing()) {
                    App.saleView = new SaleView();
                } else {
                    App.saleView.setState(JFrame.NORMAL);
                    App.saleView.toFront();
                    App.saleView.requestFocusInWindow();
                }
            }
            else {
                JOptionPane.showMessageDialog(contentPane, "当前用户没有该权限", "权限不足！", JOptionPane.ERROR_MESSAGE);
            }
        });
        //退出登录
        item2.addActionListener(e -> {
            App.loggedUser = new User();
            dispose();
            if (App.saleView != null){
                App.saleView.dispose();
            }
            new LoginView();
        });
        //退出
        item3.addActionListener(e -> dispose());

    }
}
